package com.example.controller;

import com.example.util.RedisLockUtils;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@Slf4j
@RestController
public class TestController {

    @Autowired
    RedisTemplate redisTemplate;
    @GetMapping(value = "/redisson/{key}")
    public String redissonTest(@PathVariable("key") String lockKey) {
        RLock rLock = RedisLockUtils.getLock(RedisLockUtils.getLockKey(lockKey));
        if (RedisLockUtils.tryLock(rLock, 1, 10)) {
            try {
                Thread.sleep(15000);
                return "处理成功";
            } catch (Exception e) {
                return e.getMessage();
            }
        } else {
            return "处理失败";
        }
    }
}
